layui.extend({
    'tableSelect': [window.ROOT_URL + '/static/admin/js/utils/table-select/tableSelect'],
});
define(['layui'], function () {
    return function ($element, container) {
        layui.use('tableSelect',function () {
            var tableSelect = layui.tableSelect;
            $element.map(function (index, elem) {
                    var field = $(elem).data('field') //显示字段名称
                        , type = $(elem).get(0).hasAttribute('multiple') ? 'checkbox' : 'radio'
                        , keywords = $(elem).data('keywords') || 'keywords' //关键词字段名称
                        , pk = $(elem).data('pk') || 'id' //主键
                        , placeholder = $(elem).data('placeholder') || '关键词搜索'
                        , source = $(elem).data('source')
                        , field_title = $(elem).data('title') || '名称'
                        , where = $(elem).data('where') || ''
                        , extend = $(elem).data('extend') || []
                        , cols = [
                        {type: type}
                        , {field: pk, title: 'ID', width: 100}
                        , {field: field, title: field_title, width: 240}
                    ];
                    $(elem).attr('ts-selected') || $(elem).attr('ts-selected', $(elem).val());
                    if (extend.length > 0) {
                        cols = cols.concat(extend);
                    }
                    var search_fields = [];
                    cols.map(function (item) {
                        item.field && search_fields.push(item.field);
                    });
                    if (!source) return;
                    var new_elem = $(elem).clone().attr('type', 'hidden');
                    $(elem).removeAttr('name').attr('readonly', true).after(new_elem);
                    var page = Object.getOwnPropertyNames(cols).length > 5 ? true : {
                        layout: ['prev', 'next']
                        , 'prev': '上一页'
                        , 'next': '下一页'
                    };
                    var param = {
                        fields: search_fields.join(',')
                    };
                    if(where){
                        param.where = where;
                    }
                    tableSelect.render({
                        container: container,
                        elem: elem,
                        checkedKey: pk, //表格的唯一建值，非常重要，影响到选中状态 必填
                        searchKey: keywords,	//搜索输入框的name值
                        searchPlaceholder: placeholder,
                        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                            url: source,
                            cols: [cols],
                            page: page,
                            parseData: function (res) { //res 即为原始返回的数据
                                return {
                                    "code": res.code, //解析接口状态
                                    "msg": res.msg, //解析提示文本
                                    "count": res.data.count, //解析数据长度
                                    "data": res.data.list //解析数据列表
                                }
                            }
                            , response: { //重新规定返回的数据格式
                                statusName: 'code'
                                , statusCode: 1
                            },
                            where: param
                        }
                        , done: function (elem, result) {
                            if (!result.data.length) {
                                return layui.layer.msg('没有选择任何数据!')
                            }
                            this.setValues(result.data);
                        }
                        , setValues: function (data) {
                            var titles = [], ids = [];
                            data.map(function (item) {
                                titles.push(item[field]);
                                ids.push(item[pk]);
                            });
                            $(elem).val(titles.join(','));
                            new_elem.val(ids.join(','));
                        }
                    })
                }
            );
        });
    }
});